/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller.converters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import model.domain.Person;

/**
 *
 * @author damile
 */
public class UserConverter implements Converter {

    public UserConverter(){
        
    }


@Override
    public Object getAsObject(FacesContext context, UIComponent component, String value)
    {

        if (value == null || (value.trim().length() == 0))
        {
            return value;
        }

        Person user = new Person();

        String[] splitedString = value.split(" - ");
        user.setPersonID(Integer.parseInt(splitedString[0]));
        user.setFirstName(splitedString[1]);
        user.setLastName(splitedString[2]);

        return user;
    }

    @Override
      public String getAsString(FacesContext context, UIComponent component, Object value)
      {

       Person user = null;

        if (value instanceof Person)
        {
            user = (Person)value;

            StringBuilder userAsString = new StringBuilder();
            userAsString.append(user.getPersonID());
            userAsString.append(" - ");
            userAsString.append(user.getFirstName());
            userAsString.append(" - ");
            userAsString.append(user.getLastName());
            String string = userAsString.toString();

            return string;
        }
        return "";
    }

}
